package com.toune.movie.mvp.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.toune.movie.R;
import com.toune.movie.base.Api;
import com.toune.movie.base.AppConstant;
import com.toune.movie.base.BaseFragment;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.base.myUtil.NoHttpCallBack;
import com.toune.movie.base.myUtil.RequstUtils;
import com.toune.movie.mvp.activity.DownListActivity;
import com.toune.movie.mvp.adapter.AdapterMusicVo;
import com.toune.movie.mvp.adapter.AdapterMusicWebSets;
import com.toune.movie.mvp.contract.MusicFragmentContract;
import com.toune.movie.mvp.presenter.MusicFragmentPresenter;
import com.toune.movie.vo.MusicVo;
import com.toune.movie.vo.MusicWebSitesVo;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MusicFragment extends BaseFragment<MusicFragmentPresenter> implements MusicFragmentContract.View {

    private static MusicFragment fragment;
    @BindView(R.id.my_down_iv)
    ImageView myDownIv;
    @BindView(R.id.music_et)
    EditText musicEt;
    @BindView(R.id.rclview)
    RecyclerView rclview;
    @BindView(R.id.content_ll)
    LinearLayout contentLl;
    @BindView(R.id.iv_slide)
    ImageView ivSlide;
    @BindView(R.id.textView8)
    TextView textView8;
    @BindView(R.id.handle)
    LinearLayout handle;
    @BindView(R.id.textView14)
    TextView textView14;
    @BindView(R.id.mLreclView)
    RecyclerView mLreclView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.music_ll)
    LinearLayout musicLl;
    @BindView(R.id.sliding_drawer)
    SlidingDrawer slidingDrawer;
    Unbinder unbinder;
    private List<MusicWebSitesVo> webSitesVos;
    private int page = 1;
    private AdapterMusicVo adapterMusicVo;
    private AdapterMusicWebSets adapterMusicWebSets;
    private boolean flag;

    public static MusicFragment newInstance() {
        if (fragment == null) {
            fragment = new MusicFragment();
        }
        return fragment;
    }

    @Override // 实例化presenter
    public MusicFragmentPresenter initPresenter() {
        return new MusicFragmentPresenter();
    }

    @Override
    public void loadMore() {
        page++;
        searchMusic();
    }

    @Override
    public void refreData() {
        page = 1;
        searchMusic();
    }

    @Override
    protected void initData() {
    }

    int tag = 0;

    @Override
    public void init(Bundle savedInstanceState) {
        initeMusicWebSites();
        myDownIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxActivityTool.skipActivity(getContext(), DownListActivity.class);
            }
        });
        slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                flag = true;
                ivSlide.setImageResource(R.drawable.slibe_down);
                searchMusic();
            }
        });
        slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                flag = false;
                ivSlide.setImageResource(R.drawable.slibe_up);
            }
        });
        slidingDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {
            @Override
            public void onScrollEnded() {
            }

            @Override
            public void onScrollStarted() {
            }
        });
    }


    private void initeMusicWebSites() {
        webSitesVos = new ArrayList<>();
        for (int i = 0; i < AppConstant.MUSIC_WEBSITES.length; i++) {
            MusicWebSitesVo musicWebSitesVo = new MusicWebSitesVo();
            musicWebSitesVo.setName(AppConstant.MUSIC_WEBSITES[i]);
            musicWebSitesVo.setKey(AppConstant.MUSIC_WEBSITE_TAGS[i]);
            webSitesVos.add(musicWebSitesVo);
        }
        adapterMusicWebSets = new AdapterMusicWebSets(R.layout.adapter_music_website, webSitesVos);
        rclview.setLayoutManager(new GridLayoutManager(getContext(), 4));
        rclview.setAdapter(adapterMusicWebSets);
        adapterMusicWebSets.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                adapterMusicWebSets.setSelectPosition(position);
                webSite = webSitesVos.get(position).getKey();
                page = 1;
            }
        });
    }

    String webSite = "netease";

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_music;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    private void searchMusic() {
        MyUtil.showProDialog(getContext());
        NoHttpCallBack.params.put("input", musicEt.getText().toString());
        NoHttpCallBack.params.put("filter", "name");
        NoHttpCallBack.params.put("type", webSite);
        NoHttpCallBack.params.put("page", page);
        RequstUtils.POSTMUSIC(Api.SearchMusicUrl, new NoHttpCallBack<MusicVo>(MusicVo.class) {
            @Override
            protected void getSucceed(MusicVo musicVo) {
                if (musicVo != null) {
                    if (page == 1) {
                        musicAll.clear();
                    }
                    notifyMusicDataAdapter(musicVo);
                }
                MyUtil.hidProDialog();
            }

            @Override
            protected void getFailed(String json) {
                MyUtil.hidProDialog();
                RxToast.error("ㄟ( ▔, ▔ )ㄏ 没有找到相关信息\"");
            }
        });
    }

    List<MusicVo.DataBean> musicAll = new ArrayList<>();

    private void notifyMusicDataAdapter(MusicVo musicVo) {
        if (musicVo.getData() != null) {
            musicAll.addAll(musicVo.getData());
        }
        if (adapterMusicVo == null) {
            adapterMusicVo = new AdapterMusicVo(R.layout.adapter_music, musicAll);
            mLreclView.setLayoutManager(new GridLayoutManager(getContext(), 2));
            mLreclView.setAdapter(adapterMusicVo);
        } else {
            adapterMusicVo.notifyDataSetChanged();
        }
    }
}
